package com.boxuegu.entry;

import com.boxuegu.controller.AdminController;
import com.boxuegu.controller.UserController;
import com.boxuegu.service.AdminService;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class SupermarketEntry {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);
        String choice;
        while (true) {
            System.out.println("=================欢迎来到水果超市管理系统=================");
            System.out.println("请输入您的角色：   ①顾客角色       ②管理员角色      ③退出");
            //接受用户键盘输入的选择
            choice = sc.next();
            switch (choice) {
                case "1":
                    //顾客角色
                    //创建UserController对象，并调用start方法打开用户系统
                    UserController userController = new UserController();
                    userController.start();
                    break;
                case "2":
                    //管理员角色
                    AdminController adminController = new AdminController();
                    boolean loginSuccess = login();
                    if (loginSuccess) {
                        adminController.start();
                        break;
                    } else {
                        break;
                    }
                case "3":
                    //结束程序
                    System.out.println("感谢您的使用，再见！");
                    System.exit(0);
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;

            }
        }
    }

    private static boolean login() throws IOException, ClassNotFoundException {
        //键盘接收登录信息
        Scanner sc = new Scanner(System.in);
        AdminService adminService = new AdminService();
        String id;  //用户ID
        String password;  //用户密码
        boolean loginSuccess = false;
        signForLogin:
        while (true) {
            System.out.println("请输入账号（输入n返回）");
            id = sc.next();
            if (id.equals("n")) {
                break signForLogin;  //如果输入了n，退出此次判断
            } else {
                System.out.println("请输入密码（输入n返回）");
                password = sc.next();
                if (password.equals("n")) {
                    break;
                } else {
                    boolean flag = adminService.checkPassword(id, password); // 判断admin管理员用户的用户名和密码对不对
                    if (flag) {
                        System.out.println("登陆成功！");
                        //登录成功后调用进入已登录界面的方法
                        loginSuccess = true;
                        return loginSuccess;
                    } else {
                        System.out.println("账号或密码输入错误，请重新输入");
                    }
                }
            }
        }
        return loginSuccess;
    }
}
